package cn.edu.zafu.bigdata.Yum;

import java.io.*;
import java.util.*;

public class StudentManager {
    private Map<String, Student> students = new HashMap<>();
    private static final String FILENAME = "students.ser";

    public StudentManager() {
        loadStudents();
    }

    public void displayAllStudents() {
        if (students.isEmpty()) {
            System.out.println("没有学生信息可显示。");
        } else {
            System.out.println("所有学生信息：");
            for (Student student : students.values()) {
                System.out.println(student);
            }
        }
    }

    public void addStudent() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入学号：");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                return;
            }
            System.out.print("请输入姓名：");
            String name = scanner.nextLine();
            System.out.print("请输入电话：");
            String phone = scanner.nextLine();
            if (students.containsKey(id)) {
                System.out.println("学号重复，请重新输入。");
            } else {
                students.put(id, new Student(id, name, phone));
                saveStudents();
                System.out.println("学生信息添加成功。");
                //break;
            }
        }
    }

    public void retrieveStudentInfo() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入学号检索学生信息（输入'QUIT'退出）：");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                return;
            }
            Student student = students.get(id);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生");
            }
        }
    }

    public void updateStudentInfo() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入学号更新学生信息（输入'QUIT'退出）：");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                return;
            }
            Student student = students.get(id);
            if (student != null) {
                System.out.print("请输入新的姓名：");
                String name = scanner.nextLine();
                System.out.print("请输入新的电话：");
                String phone = scanner.nextLine();
                student.setName(name);
                student.setPhone(phone);
                saveStudents();
                System.out.println("学生信息更新成功。");
            } else {
                System.out.println("未找到该学生");
            }
        }
    }

    public void deleteStudentInfo() {
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.print("请输入学号删除学生信息（输入'QUIT'退出）：");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                return;
            }
            if (students.remove(id) != null) {
                saveStudents();
                System.out.println("学生信息删除成功。");
            } else {
                System.out.println("未找到该学生");
            }
        }
    }

    public void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILENAME))) {
            oos.writeObject(students);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void loadStudents() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILENAME))) {
            students = (Map<String, Student>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            students = new HashMap<>(); // 如果文件不存在或发生错误，则初始化一个空的HashMap
        }
    }
}